package com.weetop.cfw.mine.activity

import android.os.Bundle
import android.view.View
import com.blankj.utilcode.util.ActivityUtils
import com.kongzue.dialog.v3.MessageDialog
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import com.weetop.cfw.R
import com.weetop.cfw.base.presenter.imp.CloudCurriculumBrowseRecordPresenterImp
import com.weetop.cfw.base.ui.activity.CommonBaseActivity
import com.weetop.cfw.base.view.CloudCurriculumBrowseRecordView
import com.weetop.cfw.bean.AudioVideoBrowsingRecordBean
import com.weetop.cfw.bean.TestBean
import com.weetop.cfw.mine.adapter.CloudCurriculumAdapter
import com.weetop.cfw.other.CourseDetailWebViewActivity
import kotlinx.android.synthetic.main.activity_cloud_curriculum_browse_record.*


/**
 * 商业云课浏览记录
 */
class CloudCurriculumBrowseRecordActivity : CommonBaseActivity(),View.OnClickListener,CloudCurriculumBrowseRecordView, OnRefreshLoadMoreListener {

    override fun onClick(p0: View?) {
        when(p0?.id){
            R.id.imageCloudCurriculumBrowseRecordBack -> ActivityUtils.finishActivity(this)
            R.id.textClearRecord -> tipUserClearCloudCurriculumRecord()
        }
    }
    private lateinit var cloudCurriculumAdapter: CloudCurriculumAdapter

    private var currentPageNumber = 1//当前的数据页数
    private var totalCount = 0//数据的总数量

    private var isRefresh = false//是否正在刷新
    private var isLoadMore = false//是否加载更多

    private val audioVideoBrowsingRecordBeanList = ArrayList<AudioVideoBrowsingRecordBean.DataBean>()

    private lateinit var cloudCurriculumBrowseRecordPresenterImp: CloudCurriculumBrowseRecordPresenterImp

    override fun getLayoutId() = R.layout.activity_cloud_curriculum_browse_record

    override fun initView(savedInstanceState: Bundle?) {
        cloudCurriculumBrowseRecordPresenterImp = CloudCurriculumBrowseRecordPresenterImp()
        cloudCurriculumBrowseRecordPresenterImp.attachView(this)

        smartRefreshLayout.setOnRefreshLoadMoreListener(this)
        smartRefreshLayout.setEnableAutoLoadMore(false)
       setViewsOnClickListener(this,imageCloudCurriculumBrowseRecordBack,textClearRecord)
    }

    override fun initData(savedInstanceState: Bundle?) {

        cloudCurriculumBrowseRecordPresenterImp.getVideoBrowsingRecord(this,currentPageNumber)

        cloudCurriculumAdapter = CloudCurriculumAdapter(R.layout.layout_item_my_cloud_curriculum,audioVideoBrowsingRecordBeanList)
        cloudCurriculumAdapter.emptyView = emptyView
        cloudCurriculumAdapter.setOnItemClickListener { adapter, view, position ->
           CourseDetailActivity.startCourseDetailActivity(this,audioVideoBrowsingRecordBeanList[position].infoid,0)
        }
        cloudCurriculumBrowseRecordRV.adapter = cloudCurriculumAdapter
    }

    /**
     * 商业云课浏览记录清除成功
     */
    override fun clearCloudCurriculumBrowseRecordSuccess() {
        smartRefreshLayout.autoRefresh()
    }

    /**
     * 提醒用户是否清空商业云课的浏览记录
     */
    private fun tipUserClearCloudCurriculumRecord(){
        MessageDialog.show(this,"提示","确定清空商业云课浏览记录？","清空","取消").setOnOkButtonClickListener { baseDialog, v ->
            cloudCurriculumBrowseRecordPresenterImp.clearCloudCurriculumBrowseRecord(this@CloudCurriculumBrowseRecordActivity)
            baseDialog.doDismiss()
            true
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        cloudCurriculumBrowseRecordPresenterImp.detachView()
    }

    /**
     * 云课程浏览记录获取成功
     */
    override fun getVideoBrowsingRecordSuccess(audioVideoBrowsingRecordBean: AudioVideoBrowsingRecordBean?) {
      if(audioVideoBrowsingRecordBean != null){
          totalCount = audioVideoBrowsingRecordBean.records
          if(isRefresh){
              audioVideoBrowsingRecordBeanList.clear()
              audioVideoBrowsingRecordBeanList.addAll(audioVideoBrowsingRecordBean.data)
              cloudCurriculumAdapter.notifyDataSetChanged()
              smartRefreshLayout.finishRefresh(true)
              if(audioVideoBrowsingRecordBean.data.size > 0){
                  currentPageNumber = 1//将当前数据页数重置为1
              }
              isRefresh = false
              return
          }
          if(isLoadMore){
              audioVideoBrowsingRecordBeanList.addAll(audioVideoBrowsingRecordBean.data)
              cloudCurriculumAdapter.notifyDataSetChanged()
              smartRefreshLayout.finishLoadMore(false)
              if(audioVideoBrowsingRecordBean.data.size > 0){
                  currentPageNumber += 1//将当前数据页数增加1
              }
              isLoadMore = false
              return
          }
          audioVideoBrowsingRecordBeanList.addAll(audioVideoBrowsingRecordBean.data)
          cloudCurriculumAdapter.notifyDataSetChanged()
      }
    }

    /**
     * 云课程浏览记录获取失败
     */
    override fun getVideoBrowsingRecordFailed() {
        smartRefreshLayout.finishLoadMore(false)
        smartRefreshLayout.finishRefresh(false)
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        if(totalCount <= audioVideoBrowsingRecordBeanList.size){
            showNativeShortToast("暂无更多数据")
            smartRefreshLayout.finishLoadMoreWithNoMoreData()
        }else{
            isLoadMore = true
            cloudCurriculumBrowseRecordPresenterImp.getVideoBrowsingRecord(this,currentPageNumber + 1)
        }
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        isRefresh = true
        cloudCurriculumBrowseRecordPresenterImp.getVideoBrowsingRecord(this,1)
    }

}
